package cn.spike.basic.service.impl;

import cn.spike.basic.mapper.BaseMapper;
import cn.spike.basic.query.BaseQuery;
import cn.spike.basic.service.BaseService;
import cn.spike.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：部门
 * @author:sea
 * @create:2023-01-09 12:53
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> pageQuery(BaseQuery baseQuery) {
        //查询出数据总数
        Integer totals = baseMapper.queryCount(baseQuery);
        //如果为零说明没有数据
        if(totals==0){
            //返回一个默认的查询数据
            return new PageList<T>();
        }
        //不为零 则 查询出高级分页数据
        List<T> data = baseMapper.queryData(baseQuery);
        //有参构造赋值进去 返回QueryResult
        return new PageList<T>(data,totals);
    }

}
